package com.gitee.qdbp.general.system.biz.personnel.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.gitee.qdbp.able.exception.ServiceException;
import com.gitee.qdbp.able.result.ResultCode;
import com.gitee.qdbp.base.system.model.IAccount;
import com.gitee.qdbp.base.system.service.IPasswordService;
import com.gitee.qdbp.general.system.api.personnel.basic.IUserCoreBasic;
import com.gitee.qdbp.general.system.api.personnel.model.UserCoreBean;
import com.gitee.qdbp.general.system.api.personnel.model.UserCoreUpdate;
import com.gitee.qdbp.general.system.api.personnel.service.IUserCoreExecutor;
import com.gitee.qdbp.general.system.api.personnel.service.IUserManageExecutor;
import com.gitee.qdbp.general.system.enums.UserState;
import com.gitee.qdbp.general.system.error.UserErrorCode;
import com.gitee.qdbp.tools.utils.VerifyTools;

/**
 * 用户管理业务接口<br>
 * 运营人员重置别人的密码/锁定/解锁别人的账号之类的操作
 *
 * @version 170214
 */
@Service
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
public class UserManageExecutor implements IUserManageExecutor {

    @Autowired
    private IUserCoreExecutor userCoreExecutor;
    @Autowired
    private IUserCoreBasic userCoreBasic;
    @Autowired
    private IPasswordService passwordService;

    private static Logger log = LoggerFactory.getLogger(UserManageExecutor.class);

    @Override
    public void resetPassword(String id, String newPassword, IAccount me) throws ServiceException {
        if (VerifyTools.isBlank(id)) {
            log.warn("参数不能为空: id");
            throw new ServiceException(ResultCode.PARAMETER_IS_REQUIRED);
        }
        if (VerifyTools.isBlank(newPassword)) {
            log.warn("参数不能为空: newPassword");
            throw new ServiceException(ResultCode.PARAMETER_IS_REQUIRED);
        }

        // 根据ID从User表查询, 并验证是否存在
        userCoreExecutor.findByIdThrowOnNotExists(id);

        // 密码加密
        String hashPassword = passwordService.create(id, newPassword);
        
        // 修改User表记录
        UserCoreUpdate where = new UserCoreUpdate();
        where.setId(id);
        where.setPassword(hashPassword);
        userCoreBasic.update(where, false);

    }

    @Override
    public void disable(String id, IAccount me) throws ServiceException {
        if (VerifyTools.isBlank(id)) {
            log.warn("参数不能为空: id");
            throw new ServiceException(ResultCode.PARAMETER_IS_REQUIRED);
        }

        // 根据ID从User表查询, 并验证是否存在
        UserCoreBean old = userCoreExecutor.findByIdThrowOnNotExists(id);

        // 验证: 不允许锁定自己
        if (me.getId().equals(id)) {
            throw new ServiceException(UserErrorCode.USER_CAN_NOT_LOCK_SELF);
        }

        // 验证: 不允许锁定超级管理员
        if (old.getSuperman()) {
            throw new ServiceException(UserErrorCode.USER_CAN_NOT_LOCK_SUPERMAN);
        }

        // 验证: 只有正常状态的可以锁定
        if (old.getUserState() != UserState.NORMAL) {
            throw new ServiceException(ResultCode.RECORD_STATE_ERROR);
        }

        // 修改User表的状态为锁定
        UserCoreUpdate where = new UserCoreUpdate();
        where.setId(id);
        where.setUserState(UserState.LOCKED);
        userCoreBasic.update(where, false);

    }

    @Override
    public void enable(String id, IAccount me) throws ServiceException {
        if (VerifyTools.isBlank(id)) {
            log.warn("参数不能为空: id");
            throw new ServiceException(ResultCode.PARAMETER_IS_REQUIRED);
        }

        // 根据ID从User表查询, 并验证是否存在
        UserCoreBean old = userCoreExecutor.findByIdThrowOnNotExists(id);

        // 验证: 只有锁定状态的可以解锁
        if (old.getUserState() != UserState.LOCKED) {
            throw new ServiceException(ResultCode.RECORD_STATE_ERROR);
        }

        // 修改User表的状态为正常
        UserCoreUpdate where = new UserCoreUpdate();
        where.setId(id);
        where.setUserState(UserState.NORMAL);
        userCoreBasic.update(where, false);

    }

}
